package com.example.horizonserver.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {

    // 队列名
    public static final String DISPATCH_QUEUE = "vehicle_dispatch_queue";
    public static final String MAINTENANCE_QUEUE = "vehicle_maintenance_queue";
    // 交换机
    public static final String EXCHANGE = "vehicle_exchange";
    // 路由键
    public static final String DISPATCH_ROUTING_KEY = "vehicle.dispatch";
    public static final String MAINTENANCE_ROUTING_KEY = "vehicle.maintenance";
    // 定义队列
    @Bean
    public Queue dispatchQueue() {
        return new Queue(DISPATCH_QUEUE, true);
    }
    @Bean
    public Queue maintenanceQueue() {
        return new Queue(MAINTENANCE_QUEUE, true);
    }
    // 定义交换机
    @Bean
    public DirectExchange exchange() {
        return new DirectExchange(EXCHANGE);
    }


    // 队列和交换机绑定
    @Bean
    public Binding bindingDispatch(Queue dispatchQueue, DirectExchange exchange) {
        return BindingBuilder.bind(dispatchQueue).to(exchange).with(DISPATCH_ROUTING_KEY);
    }

    @Bean
    public Binding bindingMaintenance(Queue maintenanceQueue, DirectExchange exchange) {
        return BindingBuilder.bind(maintenanceQueue).to(exchange).with(MAINTENANCE_ROUTING_KEY);
    }

    // 消息转换器 使用json格式 能序列化map对象等
    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }
}
